package com.baosight.sczx.zm.cd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.zm.cd.domain.ZMCDDirectory;

/**
 * ServiceZMCDDirectory.java
 * <p>Title: ServiceZMCD01.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-7-16 下午12:44:49  张志芳  Create
 *</pre>
 */
public class ServiceZMCD01 extends TreeService {

	private static String topPublicNodeId = "public";//公用目录
	private static String topSpecialNodeId = "special";//特殊目录
	private static String topPersonalNodeId = "personal";//个人目录
	private static String topDepartmentNodeId = "department";//部门目录
	
	/**
	 * 树的查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-26上午09:52:33 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String p = (String) queryInfo.get(TreeConstants.TREE_NODE);
		List children = new ArrayList();
		if (p == null) {
			p = "$";
		}
		if ("$".equals(p)) {
			children = this.getTopNodes();
		} else {
			children = this.getChildNodes(p);
		}

		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(p);
		block.setBlockMeta(initMetaData());
		block.setRows(children);
		return outInfo;
	}
	
	/**
	 * 获取子节点
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-24下午05:09:27 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public List getChildNodes(String args) {
		String[] ps=args.split("-");
		String type=ps[1];
		if(type.equals("指定目录"))
		{
			FindLimitDir dir = new FindLimitDir();
			List<Map> Nodes = dir.getLimitNodes();
			return getSpecialChildNodes(args, Nodes);
		}
		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put(TreeConstants.TREE_NODE, ps[0]);
		if(type.equals("个人目录"))
		{
			_argMap.put("fdDirType", type);
			_argMap.put("fdCreatorId", UserSession.getUserId().toString());
			_argMap.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString()); 
			
		}
		else if(type.equals("部门目录"))
		{
			_argMap.put("fdDirType", type);
			_argMap.put("fdDepartmentId", UserSession.getInSessionProperty("orgCode").toString());
			_argMap.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString()); 
		}
		
		List childNodes = dao.query("ZMCDDirectory.treeNodes2", _argMap);
		return childNodes;
	}

	/**
	 * 获取根节点
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-24下午05:09:27 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List getTopNodes() {
		List _topNodes = new ArrayList();
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("label", topPublicNodeId+"-"+"公用目录");
		_tempMap.put("text", "公用目录");
		_tempMap.put("limit", "-1");
		_tempMap.put("leaf", "0");
		_tempMap.put("creator", "null");
		_tempMap.put("image", "gy.png");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("label", topSpecialNodeId+"-"+"指定目录");
		_tempMap.put("text", "指定目录");
		_tempMap.put("limit", "-1");
		_tempMap.put("leaf", "0");	
		_tempMap.put("creator", "null");
		_tempMap.put("image", "ts.png");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("label", topDepartmentNodeId+"-"+"部门目录");
		_tempMap.put("text", "部门目录");
		_tempMap.put("limit", "-1");
		_tempMap.put("leaf", "0");	
		_tempMap.put("creator", "null");
		_tempMap.put("image", "bm.png");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("label", topPersonalNodeId+"-"+"个人目录");
		_tempMap.put("text", "个人目录");	
		_tempMap.put("limit", "-1");	
		_tempMap.put("leaf", "0");		
		_tempMap.put("creator", "null");
		_tempMap.put("image", "gr.png");
		_topNodes.add(_tempMap);
							
		return _topNodes;
	}

	/**
	 * 获取指定目录的叶子节点
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-29下午04:22:44 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getSpecialChildNodes(String p, List<Map> Nodes) {
		
		List<Map> childNodes = new ArrayList<Map>();	
		
		for (Iterator it = Nodes.iterator(); it.hasNext();) {
			Map node = (Map) it.next();
			if(p.indexOf(node.get("fdSuperDirId").toString())>=0)
			{
				Map map = new HashMap();
				map.put("label", node.get("fdKeyId"));
				map.put("text", node.get("fdDirName"));		
				map.put("type", "指定目录");	
				map.put("limit", node.get("fdLimit"));
				map.put("leaf", "0");
				map.put("creator", "");
				map.put("image", "ts.png");
				childNodes.add(map);
			}
		}
		
		return childNodes;
	}
	
	/**
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-24下午05:09:27 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();

		EiColumn eiColumn = new EiColumn("label");
		eiColumn.setDescName("label");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("text");
		eiColumn.setDescName("text");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("type");
		eiColumn.setDescName("type");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("limit");
		eiColumn.setDescName("limit");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("leaf");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("creator");
		eiColumn.setDescName("creator");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("image");
		eiColumn.setDescName("image");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:53:52 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {		
		inInfo.set("userId", UserSession.getUserId());
		inInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		inInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		inInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		inInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		inInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode").toString()); 
		
		return inInfo;
	}
	
	/**
	 * 修改时要做的查询
	 * <p>Function: queryDirInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-29上午09:40:53 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryDirInfo(EiInfo inInfo) {
		String fdKeyId = inInfo.get("fdDirId").toString();
		if(fdKeyId != null && !("".equals(fdKeyId)))
		{
			StaticTools.setQueryParameters(inInfo, "fdKeyId", fdKeyId, true);//添加查询条件
		}
		EiInfo outInfo = super.query(inInfo, "ZMCDDirectory.query", new ZMCDDirectory());				

		return outInfo;
	}

	/**
	 * 目录树的删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCD01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:44:06 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {	
		
		return super.delete(inInfo, "ZMCDDirectory.delete");
	}

	/**
	 * 删除前先判断是否有文档记录,是否有子目录
	 * <p>Function: isExistDoc</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-25下午12:56:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo canDelete(EiInfo inInfo)
	{	
		String id = inInfo.getString("fdDirId");
		//判断是否有子目录
		Map<String,String> map = new HashMap<String,String>();
		map.put("fdSuperDirId", id);
		List<?> list = dao.query("ZMCDDirectory.query", map);
		if(list.size()>0)
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("该目录下有子目录，不能删除！");	
			return inInfo;
		} 
		//判断是否有文档记录
		if(Documents.isExistDoc(id))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("该目录下有文档记录，不能删除！");	
			return inInfo;
		}
		HashMap hm=new HashMap();
		hm.put("fdKeyId", id);
		this.dao.delete("ZMCDDirectory.delete", hm);
		inInfo.setMsg("目录删除成功！");	
		return inInfo;
	} 
}
